package com.naihe.anderson.sms.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.adapter.SignatureMsgAdapter;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.SignatureMsg;
import com.naihe.anderson.sms.listener.OnSignatureMsgChangeListener;
import com.naihe.anderson.sms.manager.ContactMgr;
import com.naihe.anderson.sms.manager.SignatureMsgMgr;
import com.naihe.anderson.sms.widgets.DetialGallery;
import com.naihe.anderson.sms.widgets.SystemAlertDialog;

public class SignatureMsgActivity extends Activity implements OnClickListener, OnSignatureMsgChangeListener{

	public static final int REQUEST_MAKENEW_SIGNATURE_CODE = 19;
	
	DetialGallery detialGallery;//detial_gallery
	
	SignatureMsgAdapter msgAdapter;
	List<SignatureMsg> signatureMsgs;
	
	NhSmsApp nhSmsApp;
	SignatureMsgMgr signatureMsgMgr;
	ContactMgr contactMgr;
	Button btn_back;
	Button create_btn;
	
	RelativeLayout new_layout;
	ImageButton goto_create_btn;
	
	public static final int MSG_WHAT_EDIT = 0;
	public static final int MSG_WHAT_SEND = 1;
	public static final int MSG_WHAT_SEND_TRUE = 3;
//	public static final int MSG_WHAT_SEND_SUCCESS = 4;
	public static final int MSG_WHAT_DELT = 2;
	public static final int MSG_WHAT_DATACHANGE = 5;
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_WHAT_DATACHANGE:
				signatureMsgs.clear();
				signatureMsgs.addAll(signatureMsgMgr.getSignatureMsgInfo());
				if(signatureMsgs.size() == 0){
					new_layout.setVisibility(View.VISIBLE);
				} else {
					if(new_layout.isShown()) new_layout.setVisibility(View.GONE);
				}
				Collections.sort(signatureMsgs);
				msgAdapter.notifyDataSetChanged();
				break;
			case MSG_WHAT_EDIT:
				SignatureMsg signatureMsg = (SignatureMsg) msg.obj;
				Intent intent = new Intent(SignatureMsgActivity.this, MakeNewSignatureActivity.class);
				intent.putExtra("SignatureMsg", signatureMsg);
				SignatureMsgActivity.this.startActivityForResult(intent, REQUEST_MAKENEW_SIGNATURE_CODE);
				break;
			case MSG_WHAT_SEND:
				SignatureMsg forSendMsg = (SignatureMsg) msg.obj;
				Message message = mHandler.obtainMessage();
				message.what = MSG_WHAT_SEND_TRUE;
				message.obj = forSendMsg;
				new SystemAlertDialog(SignatureMsgActivity.this, mHandler, 
						getResources().getString(R.string.msg_what_send_true), 
						getResources().getString(R.string.msg_what_send_true_text), 
						getResources().getString(R.string.cancel), 
						getResources().getString(R.string.ok), message).show();
				break;
			case MSG_WHAT_DELT:
				SignatureMsg delmsg = (SignatureMsg) msg.obj;
				signatureMsgMgr.removeSignatureMsgInfo(delmsg);
				break;
			case MSG_WHAT_SEND_TRUE:
				SignatureMsg sendMsg = (SignatureMsg) msg.obj;
				nhSmsApp.getNhSmsMgr().sendSmsBySignatureMsg(sendMsg);
				
				for(SignatureMsg tempMsg : signatureMsgs){
					if(tempMsg.getSignatureMsgId() == sendMsg.getSignatureMsgId()){
						tempMsg.setSignatureMsgStatut(SignatureMsg.SIGNATUREMSG_STATUT_SEND);
						tempMsg.setSignatureMsgTime(new Date().getTime());
						signatureMsgMgr.updateSignatureMsgInfo(tempMsg, false);
						signatureMsgMgr.cancalAlarmManager(sendMsg.getSignatureMsgId());
						break;
					}
				}
				
				Collections.sort(signatureMsgs);
				msgAdapter.notifyDataSetChanged();
				break;
//			case MSG_WHAT_SEND_SUCCESS:
//				Toast.makeText(SignatureMsgActivity.this, R.string.msg_what_send_true_success, Toast.LENGTH_SHORT).show();
//				break;
			default:
				break;
			}
		}
	};
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signaturemsg_main_layout);
        
        signatureMsgs = new ArrayList<SignatureMsg>();
        
        nhSmsApp = (NhSmsApp) getApplication();
        signatureMsgMgr = nhSmsApp.getSignatureMsgMgr();
        signatureMsgMgr.setListener(this);
        contactMgr = nhSmsApp.getContactMgr();
        signatureMsgs.addAll(signatureMsgMgr.getSignatureMsgInfo());
        Collections.sort(signatureMsgs);
        initView();
    }
	
	
	public void initView(){
		new_layout = (RelativeLayout) findViewById(R.id.new_layout);
		if(signatureMsgs.size() == 0){
			new_layout.setVisibility(View.VISIBLE);
		} else {
			new_layout.setVisibility(View.GONE);
		}
		
		goto_create_btn = (ImageButton) findViewById(R.id.goto_create_btn);
		goto_create_btn.setOnClickListener(this);
		
		btn_back = (Button) findViewById(R.id.back_btn);
		btn_back.setOnClickListener(this);
		create_btn = (Button) findViewById(R.id.create_btn);
		create_btn.setOnClickListener(this);
		detialGallery = (DetialGallery) findViewById(R.id.detial_gallery);
		detialGallery.setSpacing(-40);
		detialGallery.setUnselectedAlpha(1.0f);
		
		msgAdapter = new SignatureMsgAdapter(SignatureMsgActivity.this, signatureMsgs, mHandler);
		detialGallery.setAdapter(msgAdapter);
	}

	public void finishTing() {
		signatureMsgMgr.setListener(null);
		finish();
		System.gc();
	}
	
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.back_btn){
			finishTing();
		} else if(v.getId() == R.id.create_btn || v.getId() == R.id.goto_create_btn){
			Intent intent = new Intent(SignatureMsgActivity.this, MakeNewSignatureActivity.class);
			SignatureMsgActivity.this.startActivityForResult(intent, REQUEST_MAKENEW_SIGNATURE_CODE);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(data == null || data.getExtras() == null)
			return;
		if (requestCode == REQUEST_MAKENEW_SIGNATURE_CODE) {
			if(resultCode == RESULT_OK){
				SignatureMsg signatureMsg = (SignatureMsg) data.getExtras().get("SignatureMsg");
				signatureMsgMgr.saveSignatureMsgInfo(signatureMsg);
			} else if(resultCode == RESULT_FIRST_USER){
				SignatureMsg signatureMsg = (SignatureMsg) data.getExtras().get("SignatureMsg");
				signatureMsgMgr.updateSignatureMsgInfo(signatureMsg, true);
			}
		}
	}


	@Override
	public void OnSignatureMsgChange() {
		mHandler.sendEmptyMessage(MSG_WHAT_DATACHANGE);
	}

}
